package com.invengo.module_robot.ui.fragment.advertInfo;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.invengo.library_orionstar.RobotConstant;
import com.invengo.library_orionstar.RobotManager;
import com.invengo.module_robot.BR;
import com.invengo.module_robot.R;
import com.invengo.module_robot.databinding.FragmentAdvertInfoBinding;
import com.invengo.module_robot.ui.weiget.robustwebview.RobustWebView;
import com.invengo.module_robot.ui.weiget.robustwebview.WebViewCacheHolder;
import com.invengo.module_robot.ui.weiget.robustwebview.WebViewListener;

import me.goldze.mvvmhabit.base.BaseFragment;

public class AdvertInfoFragment extends BaseFragment<FragmentAdvertInfoBinding, AdvertInfoViewModel> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_advert_info;
    }
    @Override
    public void onDestroyView() {
        binding.speechView.unRegister();
        super.onDestroyView();
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        RobotManager.getInstance().changeTip(RobotConstant.TIP_INFO);
        String activityUrl = getArguments().getString("activity_url");
        RobustWebView webView = WebViewCacheHolder.INSTANCE.acquireWebViewInternal(getActivity());
        webView.setWebViewListener(new WebViewListener() {
            @Override
            public void onProgressChanged(@NonNull RobustWebView webView, int progress) {
                if (viewModel!=null)
                    viewModel.getUC().getShowRobotLoadingEvent().call();
            }

            @Override
            public void onReceivedTitle(@NonNull RobustWebView webView, @NonNull String title) {
            }

            @Override
            public void onPageFinished(@NonNull RobustWebView webView, @NonNull String url) {
                if (viewModel!=null)
                    viewModel.getUC().getHideRobotLoadingEvent().call();
            }
        });
        binding.layout.addView(webView);
        webView.loadUrl(activityUrl);
    }

    @Override
    public void initViewObservable() {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        WebViewCacheHolder.INSTANCE.prepareWebView();
    }
}
